package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.dto.HqConsumeExportyDto;
import com.qfedu.hotelssm.entity.HqConsume;
import com.qfedu.hotelssm.service.intf.HqConsumeService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zlp
 * @date 2022/8/28 13:51
 */
@RestController
@RequestMapping("/api/consume/")
public class HqConsumeController {
    @Autowired
    private HqConsumeService service;


    //新增
    @PostMapping("save.do")
    public R save(HqConsume consume){
        return service.save(consume);
    }
    //更新
    @PostMapping("update.do")
    public R update(HqConsume consume){
        return service.update(consume);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String name, int page, int limit){
        return service.pageByName(name,page,limit);
    }
    //导出数据
     @GetMapping("download.do")
    public  void download(HttpServletResponse response) throws IOException {
        //1.设置响应头
         response.setHeader("content-disposition","attachment;filename=consume="+System.currentTimeMillis()+".xlsx");
         //从数据库中获取数据
         List<HqConsumeExportyDto> list=service.all();
//3.实现数据导出Excel格式
         EasyExcel.write(response.getOutputStream(),HqConsumeExportyDto.class).sheet("consume").doWrite(list);
     }
    //导出数据
     @PostMapping("upload.do")
    public  R upload(MultipartFile file) throws IOException {
        return service.saveBatch(file);
     }
    //删除
    @PostMapping("delbyids.do")
    public R delByIds(int[] ids){
        return service.delByIds(ids);
    }


}
